//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module
stc_signal_t s_stcSignal[MAX_SIGNAL_NUM];

/* Debounce times of the signal */
static const uint8_t s_u8DebounceTable[MAX_SIGNAL_NUM] =
{
    /* Debounce count of each singnal */
    3U, 3U
};

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module
static bool_t srv_signal_getSta(en_signal_list_t signal_index);
static void srv_signal_debounceFun(uint8_t sta, uint8_t index, uint8_t dly_cnts);

//----------------------------------------------------------------------------------------
/**
 * @brief  heck the state of selected physical signal.
 * @param  signal_index - physical signal.
 * @return The state of selected physical signal.
 * @note   none
 */
static bool_t srv_signal_getSta(en_signal_list_t signal_index)
{
    uint8_t sta = false;

    switch (signal_index)
    {			
				case MCU_CHARGER_IN:
				{
						if (CHG_DET_READ() == 1)
						{
							  sta = true;
						}
						else 
						{
							  sta = false;
						}
						break;
				}
				
				case MCU_LOAD_IN:
				{
						if (LOAD_DET_READ() == 0)
						{
							  sta = true;
						}
						else 
						{
							  sta = false;
						}
						break;
				}

        default:
        {
            sta = false;

            break;
        }
    }

    return (sta);
}

/**
 * @brief  handle debounce function.
 * @param  sta - The state of the current physical signal.
 *         index - Point to the physical signal.
 *         dly_cnts - delay counts
 * @return none
 * @note   none
 */
static void srv_signal_debounceFun(uint8_t sta, uint8_t index, uint8_t dly_cnts)
{
    if (sta)
    {
				if (s_stcSignal[index].u8state !=  true)
				{
						s_stcSignal[index].u8validCnts++;
						if (s_stcSignal[index].u8validCnts >= dly_cnts)
						{
	            s_stcSignal[index].u8validCnts = 0;
							s_stcSignal[index].u8state = true;						
						}
				}
				else
				{
						s_stcSignal[index].u8validCnts = 0;
				}
    }
    else
    {
				if (s_stcSignal[index].u8state !=  false)
				{
						s_stcSignal[index].u8validCnts++;
						if (s_stcSignal[index].u8validCnts >= dly_cnts)
						{
	            s_stcSignal[index].u8validCnts = 0;
							s_stcSignal[index].u8state = false;						
						}
				}
				else
				{
						s_stcSignal[index].u8validCnts = 0;
				}
    }
}

/**
 * @brief  update signal active
 * @param  none
 * @return none
 * @note   none
 */
void srv_signal_updateActive(void)
{
    uint8_t index;
    uint8_t sta;
    en_signal_list_t signal;

    for (index = 0; index < MAX_SIGNAL_NUM; index++)
    {
        signal = (en_signal_list_t)index;

        sta = srv_signal_getSta(signal);

        srv_signal_debounceFun(sta, index, s_u8DebounceTable[index]);
    }
}

/**
 * @brief  get charger in
 * @param  none
 * @return sta
 * @note   none
 */
bool_t srv_signal_getChargeIn(void)
{
    return (s_stcSignal[MCU_CHARGER_IN].u8state);
}

/**
 * @brief  get load in
 * @param  none
 * @return sta
 * @note   none
 */
bool_t srv_signal_getLoadIn(void)
{
    return (s_stcSignal[MCU_LOAD_IN].u8state);
}

/**
 * @brief  This function init I/O module
 * @param  none
 * @return none
 * @note   none
 */
void srv_signal_init(void)
{
	  uint8_t index;
	
    (void)memset((void*)&s_stcSignal, 0, sizeof(s_stcSignal));
	
    /* Signal is valid by default */
    for (index = 0; index < MAX_SIGNAL_NUM; index++)
    {
        s_stcSignal[index].u8state = true;
			  s_stcSignal[index].u8validCnts = s_u8DebounceTable[index];
    }


	
    /* Turn OFF CHG MOS */
    /* Turn OFF DSG MOS */
    /* Turn OFF DSG PRO MOS */	
}

// =======================================================================================
// End of file.
// =======================================================================================
